﻿using UnityEngine;
using System.Collections;

public class SetBoundingBoxSize : MonoBehaviour {

	BoxCollider2D leftWall;
	BoxCollider2D rightWall;
	BoxCollider2D topWall;
	BoxCollider2D bottomWall;
	Transform leftWallTrans;
	Transform rightWallTrans;
	Transform topWallTrans;
	Transform bottomWallTrans;
	Camera cam;

	BoxCollider2D leftWallCollider;
	BoxCollider2D rightWallCollider;
	BoxCollider2D topWallCollider;
	BoxCollider2D bottomWallCollider;

	// Use this for initialization
	void Start () {
		this.leftWall = (BoxCollider2D)GameObject.Find ("LeftWall").GetComponent (typeof(BoxCollider2D));
		this.rightWall = (BoxCollider2D)GameObject.Find ("RightWall").GetComponent (typeof(BoxCollider2D));
		this.topWall = (BoxCollider2D)GameObject.Find ("TopWall").GetComponent (typeof(BoxCollider2D));
		this.bottomWall = (BoxCollider2D)GameObject.Find ("BottomWall").GetComponent (typeof(BoxCollider2D));

		//GameObject.DontDestroyOnLoad (this.leftWall.gameObject);
		//GameObject.DontDestroyOnLoad (this.rightWall.gameObject);
		//GameObject.DontDestroyOnLoad (this.topWall.gameObject);
		//GameObject.DontDestroyOnLoad (this.bottomWall.gameObject);

		this.leftWallCollider = (BoxCollider2D)GameObject.Find ("LeftWallCollider").GetComponent (typeof(BoxCollider2D));
		this.rightWallCollider = (BoxCollider2D)GameObject.Find ("RightWallCollider").GetComponent (typeof(BoxCollider2D));
		this.topWallCollider = (BoxCollider2D)GameObject.Find ("TopWallCollider").GetComponent (typeof(BoxCollider2D));
		this.bottomWallCollider = (BoxCollider2D)GameObject.Find ("BottomWallCollider").GetComponent (typeof(BoxCollider2D));

		//GameObject.DontDestroyOnLoad (this.leftWallCollider.gameObject);
		//GameObject.DontDestroyOnLoad (this.rightWallCollider.gameObject);
		//GameObject.DontDestroyOnLoad (this.topWallCollider.gameObject);
		//GameObject.DontDestroyOnLoad (this.bottomWallCollider.gameObject);

		this.leftWallTrans = this.leftWall.transform;
		this.rightWallTrans = this.rightWall.transform;
		this.topWallTrans = this.topWall.transform;
		this.bottomWallTrans = this.bottomWall.transform;

		cam = (Camera)GameObject.Find ("Main Camera").GetComponent(typeof(Camera));

		int width = Screen.width;
		int height = Screen.height;
		Vector3 result = cam.ScreenToWorldPoint (new Vector3 (width, height, 0f));
		float worldX = result.x;
		float worldY = result.y;

		this.leftWallTrans.position = new Vector3 (-1f * worldX, 0f, -10f);
		this.rightWallTrans.position = new Vector3 (worldX, 0f, -10f);
		this.topWallTrans.position = new Vector3 (0f, worldY, -10f);
		this.bottomWallTrans.position = new Vector3 (0f, -1f * worldY, -10f);

		this.leftWall.size = new Vector2 (0.1f, 2f * worldY);
		this.rightWall.size = new Vector2 (0.1f, 2f * worldY);
		this.topWall.size = new Vector2 (2f * worldX, 0.1f);
		this.bottomWall.size = new Vector2 (2f * worldX, 0.1f);

		this.leftWallCollider.size = this.leftWall.size;
		this.rightWallCollider.size = this.rightWall.size;
		this.topWallCollider.size = this.topWall.size;
		this.bottomWallCollider.size = this.bottomWall.size;

		Vector3 pos = Vector3.zero;
		pos = this.leftWall.transform.position;
		pos.x -= 0.2f;
		this.leftWallCollider.transform.position = pos;

		pos = this.rightWall.transform.position;
		pos.x += 0.2f;
		this.rightWallCollider.transform.position = pos;

		pos = this.topWall.transform.position;
		pos.y += 0.2f;
		this.topWallCollider.transform.position = pos;

		pos = this.bottomWall.transform.position;
		pos.y -= 0.2f;
		this.bottomWallCollider.transform.position = pos;
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}
